﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using LibMessage;
using LibMessage.Models;

namespace Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private bool stopReconnect = false;
        private bool autoLogin = true;

        private void Button1_Click(object sender, EventArgs e)
        {
            NetService.LoginAync(tbxUserName.Text);
        }

        private void AppendText(string s)
        {
            try
            {
                void ap()
                {
                    tbxRecv.AppendText(Environment.NewLine + s);
                }
                if (InvokeRequired)
                    Invoke(new MethodInvoker(() => ap()));
                else
                    ap();
            }
            catch (Exception)
            {
            }
        }

        // 连接服务器
        private void Button3_Click(object sender, EventArgs e)
        {
            // 连接服务器,成功失败都会调用Connected方法
            NetService.ConnectAsync(tbxServer.Text, Convert.ToInt32(tbxPort.Text));
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            // 注册3个方法用来响应 连接/断开连接/收到数据事件
            NetService.RegisterCallback(Connected, Disconnected, DataReceived);
        }

        private void Connected(MessagePack mp)
        {
            if (mp.Result)
            {
                AppendText("连接成功");
                if (autoLogin)
                    NetService.LoginAync(tbxUserName.Text);
            }
            else
                AppendText($"连接失败,Code:{mp.ErrCode} Info:{mp.ErrString}");
        }

        private void Disconnected(MessagePack mp)
        {
            AppendText($"连接中断:{mp.ErrString}");
            if (!stopReconnect)
            {
                NetService.ConnectAsync(tbxServer.Text, Convert.ToInt32(tbxPort.Text));
            }
        }

        private void DataReceived(MessagePack mp)
        {
            Console.WriteLine(mp.Cmd);
            switch (mp.Cmd)
            {
                case "loginresult":
                    // 登录结果
                    if (mp.Result)
                        AppendText("登录成功");
                    else
                        AppendText($"登录失败,Code{mp.ErrCode}, Info:{mp.ErrString}");
                    break;

                case "wcast":
                    // 广播数据
                    string str = Encoding.UTF8.GetString(mp.Data);
                    AppendText(str);
                    break;

                default:
                    break;
            }
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            MessagePack mp = new MessagePack { Cmd = "publish", Data = Encoding.UTF8.GetBytes(tbxMessage.Text) };
            NetService.SendDataAsync(mp);
        }
    }
}